package com.uinnova.visualbase.service;

import com.uinnova.visualbase.dao.User;
import com.uinnova.visualbase.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserServiceImpl implements  UserService {

    @Autowired
    public UserMapper userMapper;

//   验证登陆
    public int testLogin(String username, String password) {
//        默认是0，正确，1为用户名错误，2为密码错误
        int flag = 0;
        if(username == "" || username == null){
            flag = 1;
        }else{
            ArrayList<User> list  = userMapper.queryAll();
            for(User user:list){
                if(user.getUsername().equals(username)){
                    if(password == "" || password ==null){
                        flag = 2;
                    }else{
                        //                    根据用户名查询密码
                        User user1 = new User();
                        user1.setUsername(username);
                        User user2 = userMapper.queryPassword(user1);
                        if(user2.getPassword().equals(password)){
                            flag = 0;
                        }else{
                            flag = 2;
                        }
//                    密码正确，返回0，密码错误，返回2
                    }
                }else{
                    flag = 1;
                }
            }

        }
        return flag;
    }

// 根据用户名查询信息
    public User queryByUsername(String username) {
        User user = new User();
        user.setUsername(username);
        user = userMapper.queryPassword(user);
        return user;
    }
}
